package cn.yr.clock.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.yr.clock.domain.entity.Prize;
import cn.yr.clock.domain.entity.PrizeInstance;
import cn.yr.clock.domain.vo.PrizeInstanceVo;
import cn.yr.clock.handler.CacheUserHandler;
import cn.yr.clock.mapper.PrizeInstanceMapper;
import cn.yr.clock.service.PrizeInstanceService;
import cn.yr.clock.service.PrizeService;
import cn.yr.common.exception.ret.Ret;
import cn.yr.oss.OssKit;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service("prizeInstanceService")
public class PrizeInstanceServiceImpl extends ServiceImpl<PrizeInstanceMapper, PrizeInstance> implements PrizeInstanceService {

    @Autowired
    private PrizeInstanceMapper prizeInstanceMapper;
    @Autowired
    private PrizeService prizeService;
    @Autowired
    private OssKit ossKit;
    @Autowired
    private CacheUserHandler cacheUserHandler;

    @Override
    public PrizeInstance getPrizeInstanceServiceByPrizeId(Long prizeId) {

        return prizeInstanceMapper.selectByPrizeId(prizeId);
    }

    @Override
    public List<PrizeInstanceVo> listPrize(Long userId) {
        return prizeInstanceMapper.selectPrize(userId);
    }

    @Override
    public Ret<Object> generate(Long prizeId, Integer generateNum) {
        QueryWrapper<Prize> wrapper = new QueryWrapper<>();
        wrapper.eq("prize_id",prizeId);
        int count = prizeService.count(wrapper);
        if (count == 0) {
            return Ret.error("未找到该奖券");
        }
        ArrayList<PrizeInstance> list = new ArrayList<>();
        while (generateNum > 0) {
            --generateNum;
            PrizeInstance prizeInstance = new PrizeInstance();
            prizeInstance.setCreateTime(new Date());
            prizeInstance.setPrizeId(prizeId);
            String code = RandomUtil.randomString("ABCDEFGHIJKMNOPQRSGUVWXYZ0123456789abcdefghijkmnopqrsguvwxyz", 10);
            prizeInstance.setPrizeCode(code);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            QrCodeUtil.generate(code, 300, 300, "png", outputStream);
            byte[] bytes = outputStream.toByteArray();
            String upload = ossKit.upload(bytes, code);
            prizeInstance.setCodeUrl(upload);
            list.add(prizeInstance);
        }
        saveBatch(list, 100);
        return Ret.success();
    }

    @Override
    public Ret<Object> verifyPrize(String prizeCode) {
        QueryWrapper<PrizeInstance> wrapper = new QueryWrapper<>();
        wrapper.eq("prize_code",prizeCode);
        PrizeInstance prizeInstance = this.getOne(wrapper);
        if(ObjectUtil.isNull(prizeInstance)){
            return Ret.error("未找到该奖券");
        }
        if(ObjectUtil.isNull(prizeInstance.getUserId())){
            return Ret.error("该奖券暂时无人领奖，无法核销");
        }
        if(ObjectUtil.isNotNull(prizeInstance.getVerificationTime())){
            return Ret.error("该奖券已核销");
        }
        Long userId = cacheUserHandler.getUser().getUserId();
        prizeInstance.setVerificationUser(userId);
        prizeInstance.setVerificationTime(new Date());
        boolean b = this.updateById(prizeInstance);
        return b?Ret.success("核销成功"):Ret.error("核销失败");
    }

    public static void main(String[] args) {
        System.out.println(RandomUtil.randomString("ABCDEFGHIJKMNOPQRSGUVWXYZ0123456789abcdefghijkmnopqrsguvwxyz", 8));
    }
}